// index.js
Page({
 data:{
  //查找文本
  searchinfo : '',
  //搜索框颜色
  color:'',
  //分类图标
  sort_array:[
    {
      index:0,
      picurl:'/pic/学习必备.png',
      text:'学习必备'
    },{
      index:1,
      picurl:'/pic/品质生活.png',
      text:'品质生活'
    },{
      index:2,
      picurl:'/pic/影娱数码.png',
      text:'影娱数码'
    },{
      index:3,
      picurl:'/pic/健康锻炼.png',
      text:'健康锻炼'
    },{
      index:4,
      picurl:'/pic/潮玩服饰.png',
      text:'潮玩服饰'
    },{
      index:5,
      picurl:'/pic/技能教学.png',
      text:'技能教学'
    },{
      index:6,
      picurl:'/pic/校园求助.png',
      text:'校园求助'
    },{
      index:7,
      picurl:'/pic/其他类型.png',
      text:'其他类型'
    }
  ],
  sort_part:[
    {
      index:0,
      text:"最新信息",
      border_change:''
    },{
      index:1,
      text:"学习必备",
      border_change:''
    },{
      index:2,
      text:"品质生活",
      border_change:''
    },{
      index:3,
      text:"影娱数码",
      border_change:''
    },{
      index:4,
      text:"健康锻炼",
      border_change:''
    },{
      index:5,
      text:"潮玩服饰",
      border_change:''
    },{
      index:6,
      text:"技能教学",
      border_change:''
    },{
      index:7,
      text:"其他类型",
      border_change:''
    }
  ],
  // border_change:'10rpx solid rgb(255, 0, 0)',
  topnum:0,
  itemarray:[],
  personaltarget:true,
  wxAva:"",
  userName:"",
  academy:"",
  userClass:"",
  contactPhone:"",
  contactWx:"",
  contactQq:""

 },
 onLoad:function() {
  //  console.log(this.data.sort_part)
  var that = this
   var initvalue = 'sort_part[0].border_change'
   this.setData({
     [initvalue]:'15rpx solid rgb(255, 0, 0)'
   })

  var query = wx.createSelectorQuery();
  query.select('#box_fix').boundingClientRect()
  query.exec(function (res) {
    that.setData({
      comparevalue:res[0].top
    })
    // console.log(that.data.scrollTop)
  })

  //加载最新信息

 },

 //进入页面加载最新信息
 onShow(){
   var that = this;
  wx.request({
    url: 'http://127.0.0.1:8080/item/showIndexItem',
    method:"GET",
    success(res){
      // console.log(res)
      
      var iteminfo = res.data
      for(var i=0;i<iteminfo.length;i++){
        iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
      }
      that.setData({
        itemarray:iteminfo
      })
      // console.log(that.data.itemarray)
    }
  })
 },

  /*搜索框*/
  //获取搜索文本
  searchinfo:function(e){
    var text = e.detail.value;
    this.setData({
      searchinfo : text
    })
    // console.log(this.data.searchinfo)
  },
  //输入改变边框颜色
  inputchange:function() {
    this.setData({
      color:"blue"
    })
  },
  //不输入退回边框颜色
  inputreturn:function() {
    this.setData({
      color:""
    })
  },
  //搜索功能
  search:function(e) {
    var info = this.data.searchinfo;
    if(info == ''){
      /*搜索为空提示 */
      wx.showToast({
        title: '搜索内容为空',
        icon:"error",
        duration:1000
      })
    }else{
      /*搜索成功跳转*/
      wx.navigateTo({
        url: '/pages/search/search?searchinfo='+info+'',
      })
    }
  },
  //分类信息搜索
  choosepart:function(e) {
    var index= e.currentTarget.dataset.index
    // console.log(index)
    if(index == 6){
        wx.navigateTo({
          url: '/pages/helpinfo/helpinfo',
        })
    }else{
      wx.navigateTo({
        url: '/pages/info/info?itemtype='+this.data.sort_array[index].text+'',
      })
    }
  },
  //租房信息跳转
  rentalinfo:function() {
    wx.navigateTo({
      url: '/pages/rental/rental',
    })
  },
  //公告信息跳转
  informinfo:function(e) {
    wx.navigateTo({
      url: '/pages/inform/inform',
    })
  },

  //模块选择框
  change_color:function (e) {
    for(var i=0;i<8;i++){
      var value = 'sort_part['+i+'].border_change';
      this.setData({
        [value]:''
      })
    }
    var index = e.currentTarget.dataset.index;
    var value = 'sort_part['+index+'].border_change';
    this.setData({
      [value]:'15rpx solid rgb(255, 0, 0)'
    })
    // console.log(this.data.sort_part[index].text)
    var type = this.data.sort_part[index].text;

    if(index == 0){
      this.onShow();
    }else{
      // 渲染模块信息
      var that = this;
      wx.request({
        url: 'http://127.0.0.1:8080/item/showTypeItem',
        method:"POST",
        data:{
          itemtype:type
        },
        success(res){
          // console.log(res)
          
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
          // console.log(that.data.itemarray)
        }
      })
    }
   

  },
  //商品选择查看详情页
  chooseitem(e){
    var index = e.currentTarget.dataset.index;
    var itemid = this.data.itemarray[index].itemID;
    wx.navigateTo({
      url: '/pages/iteminfo/iteminfo?itemid='+itemid,
    })
  },
  //展示用户信息
  showperson(e){
    if(wx.getStorageSync('isuser') == "yes"){
      // console.log(e)
    var index = e.currentTarget.dataset.index;
    var iteminfo = this.data.itemarray[index];
    this.setData({
      wxAva:iteminfo.wxAva,
      userName:iteminfo.userName,
      academy:iteminfo.academy,
      userClass:iteminfo.userClass,
      contactPhone:iteminfo.contactPhone,
      contactWx:iteminfo.contactWx,
      contactQq:iteminfo.contactQq,
      personaltarget:false
    })
    }else{
      wx.showToast({
        title: '身份未通过审核',
        duration: 1000,
        icon: "error",
        success: (res) => {},
      })
    }
    
  },
  cancelperson(e){
    this.setData({
      personaltarget:true
    })
  },
  //滚动固定模块选择框
  // onPageScroll: function (e) {
  //   console.log(e)
  //   this.setData({
  //     scrollTop: e.scrollTop
  //   })
  
  // },
  //回到顶部
  toTop:function(e) {
    this.setData({
      topnum:0
    })
  }
  

})
